#!/bin/bash
usage() {
    echo -e "Usage :  $0 [env] [role]"
    echo -e "    where [env] can be:"
    echo -e "\t  - qa"
    echo -e "\t  - staging"
    echo -e "\t  - prod"
    echo -e "    where [role] can be:"
    echo -e "\t  - web"
    echo -e "\t  - app"
    echo -e "\t  - all"
    exit 1
    
}

if [ $# -ne 2 ];then
   usage
fi

env=''
role=''
case $1 in
    qa|staging|prod)
    env=$1
    ;;
    *)
    usage
    ;;
esac
case $2 in
    web|app|all)
        role=$2
        ;;
    *)
        usage
        ;;
esac

if [ $role != "all" ]; then
    echo "deploying $role server..."
    ansible-playbook -i inventories/${env}/hosts.ini deploy_${role}_server.yml
    echo ""
    echo "deploy finished."
elif [ $role == "all" ]; then
    echo "deploying app server..."
    ansible-playbook -i inventories/${env}/hosts.ini deploy_app_server.yml
    echo "deploying web server..."
    ansible-playbook -i inventories/${env}/hosts.ini deploy_web_server.yml
    echo ""
    echo "deploy finished."
fi
